<?php

namespace App\Admin\Controllers;

use App\Models\Consult;
use App\Models\ConsultType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ConsultController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Consult(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('cover')->image();
            $grid->column('content');
            $grid->column('issue_time');
            $grid->column('type');
            $grid->column('author');
//            $grid->column('status')->switch();
            $grid->column('status')->select([0=>'关闭',1=>'开启']);;
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Consult(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('cover');
            $show->field('content');
            $show->field('issue_time');
            $show->field('type');
            $show->field('author');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Consult(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->file('cover')->disk('oss')->saveFullUrl();
            $form->editor('content')->required();
            $form->datetime('issue_time')->required();

            $data = ConsultType::get()->pluck('type_name','id')->toArray();
            $form->select('type')->options($data);

//            $form->text('type')->required();
            $form->text('author')->required();
            $form->radio('status')->options(['0' => '关闭', '1'=> '开启'])->default('0')->required();
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
